using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RebEngine.GameComponents;
using RebEngine.Managers;
using TomShane.Neoforce.Controls;

namespace RebellionII.GameScreens
{
    public class SplashScreen : GameScreen
    {
        private static double delta = 0.0;

        Rectangle FullscreenRectangle;
        /// <summary>
        /// Constructor.
        /// </summary>
        public SplashScreen() : base()
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.0f);
            TransitionOffTime = TimeSpan.FromSeconds(1.0f);
        }
        public override void LoadContent()
        {
            base.LoadContent();
            InputManager.Manager.Input.KeyPress +=new TomShane.Neoforce.Controls.KeyEventHandler(Input_KeyPress);
            TextureManager.AddTexture("splash");
            TextureManager.AddTexture("starbackground");
        }

        public override void Initialize()
        {
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            delta = gameTime.TotalGameTime.TotalSeconds;

            if (delta > 10.0)
            {
                ScreenManager.AddScreen(new MainMenuScreen());
                ExitScreen();
            }
            
        }

        void Input_KeyPress(object sender, TomShane.Neoforce.Controls.KeyEventArgs e)
        {
            if ((e.Key == Microsoft.Xna.Framework.Input.Keys.Space) || (e.Key == Microsoft.Xna.Framework.Input.Keys.Enter))
            {
                ScreenManager.AddScreen(new MainMenuScreen());
                InputManager.Manager.Input.KeyPress -= Input_KeyPress;
                ExitScreen();
            }
            

        }

        public override void Draw(GameTime gameTime)
        {
            //EngineManager.Device.Clear(Color.Black);
            base.Draw(gameTime);
            byte fade = TransitionAlpha;
            FullscreenRectangle = new Rectangle(0, 0, EngineManager.Device.Viewport.Width, EngineManager.Device.Viewport.Height);
           
            string tooLong = ("Press Enter/Space to Continue or wait " + ((int)(10-delta)).ToString() + " seconds");

            ScreenManager.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
            ScreenManager.SpriteBatch.Draw(TextureManager.GetTexture("splash").BaseTexture as Texture2D, FullscreenRectangle, new Color(fade,fade,fade));
            ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, tooLong, new Vector2((EngineManager.Device.Viewport.Width / 2 - (ScreenManager.Font.MeasureString(tooLong).X / 2)), (EngineManager.Device.Viewport.Height - 40)), Color.White);
            
            ScreenManager.SpriteBatch.End();

            
        }
    }
}
